package com.ltmall.api.dao.impl;

import com.ltmall.api.bean.Comment;
import com.ltmall.api.dao.IGoodsCommentDAO;
import com.ltmall.api.util.PageUtil;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;

import java.sql.SQLException;
import java.util.List;

/**
 * 商品评价DAO
 *
 * @author dou.guo
 * @Title GoodsCommentDAO.java
 * @Description 类GoodsCommentDAO.java的实现描述：类实现描述 
 * @date 2017年10月9日 下午5:56:19
 * @package com.ltmall.api.dao.impl
 */
public class GoodsCommentDAO extends BaseEntityDAO<Comment> implements IGoodsCommentDAO {

    @Override
    @SuppressWarnings({ "rawtypes", "unchecked" })
    public List<Comment> getComment(final List<Integer> goodsIDs, final PageUtil page) {

        //final String SQL = "from Comment comment where comment.goodsID in (:alist) ";
        final String SQL = "SELECT * FROM flavor_goods_comment WHERE goods_id in (:alist) ";
        List<?> list = this.getHibernateTemplate().execute(new HibernateCallback<List>() {
            public List doInHibernate(Session session) throws HibernateException, SQLException {
                Query query = session.createSQLQuery(SQL);
                query.setParameterList("alist", goodsIDs);
                // 分页
                query.setFirstResult((page.getCur_page() - 1) * page.getNumPerPage())
                    .setMaxResults(page.getNumPerPage());
                return query.list();
            }
        });

        return (List<Comment>) list;
    }

    @Override
    public boolean updateComment(Comment order) {
        String hql = "update Comment set content = ? ,goodsRating = ?,serviceRating = ? ,speedRating = ?  where commentID = ?";
        try {
            Query query = getSession().createQuery(hql);
            query.setParameter(0, order.getContent());
            query.setParameter(1, order.getGoodsRating());
            query.setParameter(2, order.getServiceRating());
            query.setParameter(3, order.getSpeedRating());
            query.setParameter(4, order.getCommentID());
            int temp = query.executeUpdate();
            if (temp == 1) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public boolean deleteComment(String commentId) {
        // TODO Auto-generated method stub
        String hql = "delete Comment oComment where oComment.commentID = ?";
        try {
            Query query = getSession().createQuery(hql);
            query.setParameter(0, new Integer(commentId));
            int temp = query.executeUpdate();
            if (temp >= 1) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public boolean insertComment(Comment order) {
        try {
            getHibernateTemplate().persist(order);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

}
